
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>FreeMarker Index</title>

    <link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/style.css"/>
    
    <script>
		function lessNum(obj)
		{
			if(parseInt(obj.parentElement.children[1].innerHTML)<=1)
			{
				obj.parentElement.children[1].innerHTML=1;
				alert("商品数量至少为1，不能再减了");
			}
			else
			{
				obj.parentElement.children[1].innerHTML=parseInt(obj.parentElement.children[1].innerHTML)-1;
			
			}
		}
		
		function moreNum(obj)
		{
			if(parseInt(obj.parentElement.children[1].innerHTML)==parseInt(obj.parentElement.children[3].innerHTML))
			{
				alert("库存不足了，不能再加了");
			}
			else
			{
				obj.parentElement.children[1].innerHTML=parseInt(obj.parentElement.children[1].innerHTML)+1;
			}
			
		}
		
		function addtoshoppingcart()
		{
			var amount = document.getElementById("allNum").innerHTML;
			var commodityId = document.getElementById("commodityId").innerHTML;
			
			var xhr = new XMLHttpRequest();
			xhr.onreadystatechange = function(){
			 if(xhr.readyState == 4){
	                var status = xhr.status;
	                if(status >= 200 && status < 300 || status == 304){
	                	var result = xhr.responseText;
	                	if(result == 'true'){
	                		window.location.href="/buyer/detail?id="+commodityId;
	                		alert("加入购物车成功");
	                		
	                	}else if(result == 'false'){
	                		alert("加入购物车失败");
	                	}
	                	else 
	                	{
	                		alert("库存不足");
	                	}
	                }else{
	                	alert("加入购物车失败");
	                }
	            }
		};
		 xhr.open('post','/addtoshoppingcart');
		 xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		 xhr.send("id="+commodityId+"&amount="+amount);
		}
		
		
		function HandleOnConfirm() {
	  		var c = confirm("确定加入购物车？");
	  		if (c) {
	    		addtoshoppingcart();
	  		}
			else
			{
			window.event;
			}
		}

	</script>
  </head>

  <body>

	
    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>

 		<div class="navbar-brand" Left="400px">买家您好，${sys_user_key.account}</div>
	          <a class="navbar-brand" href="/system/logout">[退出]</a>
        </div>
       <div id="navbar" class="navbar-collapse collapse">
          	<form class="navbar-form navbar-right">
			<a class="navbar-brand" href="/buyer/index/all">首页</a>
			
            <a class="navbar-brand" href="/buyer/account">账务</a>
            
           	<a class="navbar-brand" href="/buyer/shoppingcart">购物车</a>
 			</form>
       </div><!--/.navbar-collapse -->
      </div>
    </nav>

	<div class="g-doc">
	<div class="m-tab">
	</div>
    <div class="n-show f-cb" id="showContent">
        <div class="img"><img src="${commodity.picture}" alt="" ></div>
        <div class="cnt">
            <h2>${commodity.title}</h2>
            <p class="summary">${commodity.summary}</p>
            <div class="price">
                <span class="v-unit">¥</span><span class="v-value">${commodity.price}</span>
                <span hidden="true" id="commodityId">${commodity.id}</span>
            </div>
            <div class="num">购买数量：
	            <span id="plusNum" class="lessNum" onclick="lessNum(this)">-</span>
	            <span class="totalNum" id="allNum">1</span>
	            <span id="addNum" class="moreNum" onclick="moreNum(this)">+</span>
	            （库存<span class="totalNum">${commodity.remainningAmount}</span>&nbsp;&nbsp;&nbsp;件）
            </div>
           <#if commodity.status==1>
	             <div class="oprt f-cb">
	                <span class="u-btn u-btn-primary z-dis">已购买</span>
	                <span class="buyprice">当时购买价格：¥${commodity.preprice}</span>
	            </div>
            <#else>
	            <div class="oprt f-cb">
	                <button class="u-btn u-btn-primary" onclick="HandleOnConfirm()">
	                                              加入购物车</button>
	            </div>
           </#if>
        </div>
    </div>
    <div class="m-tab m-tab-fw m-tab-simple f-cb">
        <h2>详细信息</h2>
    </div>
    <div class="n-detail">
        ${commodity.content}
    </div>
</div>


<div class="n-foot">
 <#include "/footer.ftl">
</div>
    <!-- Bootstrap core JavaScript -->
    <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
    <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  </body>
</html>